package question.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.HashMap;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import question.bean.QuestionDetailBean;
import utils.KeyBoard;
import utils.MyLogUtils;
import utils.ToastUtils;
import view.MyImageLoader;

/**
 * Created by Administrator on 2017/4/25.
 * 发表评价用的
 */

@ContentView(R.layout.evaluate_qusetion)
public class EvaluateQuestion extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.common_title)
    private TextView common_title;
    @ViewInject(R.id.evaluate_question_answer_ima)
    private ImageView evaluate_question_answer_ima;
    @ViewInject(R.id.evaluate_question_answer_name)
    private TextView evaluate_question_answer_name;
    @ViewInject(R.id.evaluate_question_answer_detail)
    private TextView evaluate_question_answer_detail;
    @ViewInject(R.id.evaluate_question_answer_college)
    private TextView evaluate_question_answer_college;
    @ViewInject(R.id.evaluate_question_evaluate_content)
    private EditText evaluate_question_evaluate_content;
    @ViewInject(R.id.evaluate_question_evaluate_num)
    private TextView evaluate_question_evaluate_num;
    @ViewInject(R.id.evaluate_question_commit)
    private LinearLayout evaluate_question_commit;
    @ViewInject(R.id.star)
    private RatingBar star;

    private int currentCount = 0;//当前评分
    private String content;//评论内容
    private QuestionDetailBean questionDetailBean;
    private String dialogId;

    private static final String TAG = "EvaluateQuestion";

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    try {
                        JSONObject jsonObject = new JSONObject(String.valueOf(msg.obj));
                        String err_code = jsonObject.getString("err_code");
                        if("0".equals(err_code)){
                            EvaluateQuestion.this.setResult(2);
                            EvaluateQuestion.this.finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        questionDetailBean = (QuestionDetailBean) getIntent().getSerializableExtra("questionDetailBean");
        dialogId = getIntent().getStringExtra("dialogId");
        initView();
        KeyBoard.showKeyBoard();
    }

    private void initView() {
        evaluate_question_commit.setOnClickListener(this);
        common_back.setOnClickListener(this);
        common_title.setText("评价");

        if (questionDetailBean != null) {
            QuestionDetailBean.DetailBean detail = questionDetailBean.getDetail();
            MyImageLoader.showImage(detail.getAnswerer_headimgurl(), evaluate_question_answer_ima);
            evaluate_question_answer_name.setText(detail.getAnswerer_name());
            evaluate_question_answer_detail.setText(detail.getCollege() + " | " + detail.getGrade() + "届");
            evaluate_question_answer_college.setText(detail.getMajor());
        }

        star.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                currentCount = (int) rating;
                MyLogUtils.e(TAG, currentCount + "");
            }
        });
        evaluate_question_evaluate_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 255) {
                    evaluate_question_evaluate_content.setText(content);
                    evaluate_question_evaluate_content.setSelection(content.length());
                    ToastUtils.shortToast("输入字数已达上限");
                } else {
                    content = s.toString();
                }
                evaluate_question_evaluate_num.setText("" + content.length() + "/255");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        star.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                currentCount = (int) rating;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                EvaluateQuestion.this.finish();
                break;
            case R.id.evaluate_question_commit:
                comment();
                break;
            default:
                break;
        }
    }


    private void comment() {
        if (!TextUtils.isEmpty(content)) {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("dialogid", dialogId);
            hashMap.put("content", content);
            hashMap.put("score", String.valueOf(currentCount));
            new InterNetController(this, Constant.COMMENTEXPERT, handler, hashMap, 0);
        } else {
            ToastUtils.shortToast("评价内容不能为空");
        }
    }
}
